//
//  BMAScoreProgressionBarView.m
//  Autismus
//
//  Created by Salhi yacine on 15/01/12.
//  Copyright (c) 2012. All rights reserved.
//

#import "BMAScoreProgBarDataSource.h"
#import "BMAScoreProgBarDelegate.h"
#import "BMAScoreProgressionBarView.h"

#define MARGINGAME  1

@implementation BMAScoreProgressionBarView

@synthesize delegate;
@synthesize dataSource;

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self != nil) 
    {
    }
    return self;
}

- (void)drawRect:(CGRect)rect
{
    // Drawing code
    if (dataSource)
    {
        NSUInteger nbGames = [dataSource numberOfGameInProgBarView:self];
        NSUInteger x = 0;
        NSUInteger width = 0;
        for (NSUInteger i = 0; i < nbGames; ++i)
        {
            NSUInteger perc = [dataSource progBarView:self percentageValueForGameAtIndex:i];
            UIColor *color = [dataSource progBarView:self colorForGameAtIndex:i];

            CGContextRef context = UIGraphicsGetCurrentContext();
            width = self.frame.size.width * perc / 100;
            CGRect rectangle = CGRectMake(x, 0, width, self.frame.size.height);
            CGContextAddRect(context, rectangle);
            CGContextSetFillColorWithColor(context, color.CGColor);
            CGContextFillRect(context, rectangle);
            [self drawString:[NSString stringWithFormat:@"%i%%", perc] inRect:rectangle];
            x += width + MARGINGAME;
        }
    }
}

- (void)drawString:(NSString *)s inRect:(CGRect)contextRect {

    UILabel *aLabel = [[UILabel alloc] initWithFrame:contextRect];

    [aLabel setText:s];
    [aLabel setTextAlignment:UITextAlignmentCenter];
    [aLabel setTextColor:[UIColor blackColor]];
    [aLabel setBackgroundColor:[UIColor clearColor]];
    [self addSubview:aLabel];
}

@end
